<?php
App::import('Core', 'HttpSocket');
class ajaxController extends AppController {
	var $name = 'ajax';
	
	var $uses = array('Comment','Favoritearea','Users','Premiums');
	var $components = array('Auth','Bench','Apikey');
	var $auth_allow = array('getKocategory','getArticleList','getPremium');
	
	//==========================================================================
	//コールバック
	//==========================================================================
	function beforeFilter(){
		
		//AppController::beforeFilter()を処理
		parent::beforeFilter();
		
		//フォームのCSRF対策
		$this->Security->requireAuth('setArticleList','setComment');
		
		$this->Auth->autoRedirect = false;//自動リダイレクトをオフ
		
		
	}
	
	//親カテゴリーより子カテゴリーを取得する
	function getKocategory(){
		
        $this->layout = 'ajax';
		$categorylist = null;
		
        
		//APIコードを取得
		$api_Key = $this->Apikey->getApiKey(null);
					
		//カテゴリコード
		$categorycode=h($_GET['CategoryCode']);
		$param = '&article_category_code='. $categorycode;
		
		//もしもAPIで検索
		$url = 'http://api.moshimo.com/category/list.json?authorization_code='. $api_Key. $param;
		$socket = new HttpSocket();
		$categorylist = $socket->get($url);
			
		$this->set('categorylist',$categorylist);
		$this->render('getKocategory');
	}
	
	//お気に入り商品エリアをDBへ保存
	function setArticleList(){
		$this->layout = 'ajax';
		
		//入力された値を取得
		$this->Favoritearea->set($this->data);
		
		//ショップIDと現在のお気に入り商品エリアを取得
		$shop_id = $this->Auth->user('shopid');
		$favoritearea=urldecode($_POST['favoritearea']);
		
		//入力された値のチェック
		if( !empty($favoritearea)){
			//入力値が正常の場合
			
			//IDの取得
			$conds = array();
			$conds['shopid'] = $shop_id;
			$result = $this->Favoritearea->find($conds);
			
			//お気に入り商品データの追加、更新
			if(empty($result)){
				//追加
				$data = array( 'Favoritearea' => 
						array(
							'shopid' => $shop_id,
							'favoritearea' => $favoritearea));
			}
			else{
				//更新
				$data = array( 'Favoritearea' => 
						array(
							'id' => $result['Favoritearea']['id'],
							'shopid' => $shop_id,
							'favoritearea' => $favoritearea));
			}
			
			$this->Favoritearea->save($data,false);
			
			$this->set('ret',true);
		}
		else{
			//入力値異常の場合
			$this->set('ret',false);
		}
	}

	//お気に入り商品エリアをDBから取得
	function getArticleList(){
		$this->layout = 'ajax';
		
		$ret = array('status' => false, 'favoritearea' => null);
		
		//ショップIDと現在のお気に入り商品エリアを取得
		$shop_id = $this->Auth->user('shopid');
		
		//ショップIDの値のチェック
		if( !empty($shop_id)){
			//入力値済みの場合
			
			//お気に入り商品データの取得
			$conds = array();
			$conds['shopid'] = $shop_id;
			$result = $this->Favoritearea->find($conds);
			
			if(!empty($result)){
				$ret = array('status' => true, 'favoritearea' => $result['Favoritearea']['favoritearea']);
				$ret['status'] = true;
			}
		}
		$this->set('ret',$ret);
	}

	//コメントをDBから取得
	function getComment(){
		$this->layout = 'ajax';
		
		$ret = array('status' => false, 'comment' => '');
		
		//ショップIDと商品IDを取得
		$shop_id = $this->Auth->user('shopid');
		$article_id=($_GET['articleid']);
		
		//ショップIDの値のチェック
		if( !empty($shop_id)){
			//入力値済みの場合
			
			//コメントデータの取得
			$conds = array();
			$conds['shopid'] = $shop_id;
			$conds['articleid'] = $article_id;
			$result = $this->Comment->find($conds);
			
			if(!empty($result)){
				$ret = array('status' => true, 'comment' => $result['Comment']['comment']);
			}
			else{
				//存在しない場合は戻り値を正常にするため
				$ret = array('status' => true, 'comment' => '');
			}
		}
		$this->set('ret',$ret);
	}

	//お気に入り商品エリアをDBへ保存
	function setComment(){
		$this->layout = 'ajax';
		
		//入力された値を取得
		$this->Comment->set($this->data);
		
		//ショップID、商品ID、コメントを取得
		$shop_id = $this->Auth->user('shopid');
		$commentData=$_POST['commentdata'];
		$article_id=$_POST['articleid'];
		
		//入力された値のチェック
		if( !empty($shop_id)){
			//入力値が正常の場合
			
			if(empty($commentData)){
				//未入力の場合は削除
				$this->_delComment($shop_id,$article_id);
				$this->set('ret',true);
			}
			else{
				
				//IDの取得
				$conds = array();
				$conds['shopid'] = $shop_id;
				$conds['articleid'] = $article_id;
				$result = $this->Comment->find($conds);
				
				//お気に入り商品データの追加、更新
				if(empty($result)){
					//追加
					$data = array( 'Comment' => 
							array(
								'shopid' => $shop_id,
								'articleid' => $article_id,
								'comment' => $commentData));
				}
				else{
					//更新
					$data = array( 'Comment' => 
							array(
								'id' => $result['Comment']['id'],
								'shopid' => $shop_id,
								'articleid' => $article_id,
								'comment' => $commentData));
				}
				
				$this->Comment->save($data,false);
				$this->set('ret',true);
			}
		}
		else{
			//入力値異常の場合
			$this->set('ret',false);
		}
	}

	//お気に入り商品エリアをDBを削除
	function _delComment($shop_id, $article_id){
		
		$conds = array();
		$conds['shopid'] = $shop_id;
		$conds['articleid'] = $article_id;
		$result = $this->Comment->find($conds);
		
        $this->Comment->deleteAll($conds);
	}
	
	//商品情報を取得
	function getarticle(){
		$this->layout = 'ajax';
		
		//ショップIDと商品IDを取得
		$shop_id = $this->Auth->user('shopid');
		$article_id=$_GET['articleid'];
		
		//ショップIDの値のチェック
		if( !empty($shop_id)){
			//入力値済みの場合
			
			//APIコードを取得
			$api_Key = $this->Apikey->getApiKey(null);
								
			//もしもAPIで検索
			$socket = new HttpSocket();
			$param = '&article_id='. $article_id;
			$url = 'http://api.moshimo.com/article/search.json?authorization_code='. $api_Key. $param;

			$article = $socket->get($url);
		}
		$this->set('article',$article);
        $this->render('getArticle');
	}
	
	//サイト作成のため商品情報を取得
	function getHtml_Aticle(){
		$this->layout = 'ajax';
		
		//ショップIDと商品IDを取得
		$shop_id = $this->Auth->user('shopid');
		$article_id=$_GET['articleid'];
		//ショップIDの値のチェック
		if( !empty($shop_id)){
			//入力値済みの場合
			
			//APIコードを取得
			$api_Key = $this->Apikey->getApiKey($shop_id);
									
			//もしもAPIで検索
			$socket = new HttpSocket();
			$param = '&article_id='. $article_id;
			$url = 'http://api.moshimo.com/article/search.xml?authorization_code='. $api_Key. $param;

			$xmlarticle = $socket->get($url);
	        $article = simplexml_load_string($xmlarticle); 
	        $Totalcount=$article->Found;
            $i=0;
	        
			if(!($Totalcount == 0)){
				foreach($article->Articles->Article as $data){
   			   		$item[$i]['article']['ArticleId']=trim($data->ArticleId);
   			   	  	$item[$i]['article']['Name']=trim($data->Name);
           	   	  	$item[$i]['article']['Description']=trim($data->Description);
           	   	  	$item[$i]['article']['SpecialDescription']=trim($data->SpecialDescription);
           	   	  	$item[$i]['article']['Spec']=trim($data->Spec);
           	   	  	$item[$i]['article']['CatchCopy']=trim($data->CatchCopy);
           	   	  	$item[$i]['article']['ShopPrice']=trim($data->ShopPrice);
           	   	  	$item[$i]['article']['ImageCode']=trim($data->ImageCode);
           	   	  	$item[$i]['article']['ImageCount']=trim($data->ImageCount);
				}
				$item[$i]['article']['ShopId']=trim($article->Shop->ShopId);
			}
		}
		
    	$this->set('htmlarticle',$item[0]);
        $this->render('getHtmlAticle');
	}
	
	//プレミアム登録情報を取得
	function getPremium(){
		$this->layout = 'ajax';
		
		$ret['Premium'] = 0;
		$ret['status'] = false;
				
		//ショップIDを取得
		$shop_id = $this->Auth->user('shopid');
		
		//ショップIDの値のチェック
		if(!empty($shop_id)){
			//入力値済みの場合
			
			//コメントデータの取得
			$now = getdate();
			$today = date('Y-m-d h:i:s');
			
			$conds = array(
					'shopid' => $shop_id,
					'status' => 0,
					'payfromdate <= ' => $today,
					'paytodate >= ' => $today);
			
			//$conds['shopid'] = $shop_id;
			//$conds['status'] = 0;
			//$conds['payfromdate'] = '<= '. $today;
			//$conds['paytodate'] = '=> '. $today;
			$result = $this->Premiums->find($conds);
		        
			if(!empty($result)){
				//プレミアム会員
				$ret['Premium'] = '1';
				$ret['status'] = true;
			}
			else{
				//フリー会員
				$ret['Premium'] = '0';
				$ret['status'] = true;
			}
		}
		$this->set('ret',$ret);
	}
	
	//サイト作成のため複数商品情報を取得
	function getHtml_AticleList(){
		$this->layout = 'ajax';
		
		//ショップIDと商品IDを取得
		$shop_id = $this->Auth->user('shopid');
		$articleidlist=$_GET['articleidlist'];
		
		//ショップIDの値のチェック
		if( !empty($shop_id)){
			//入力値済みの場合
			
			//APIコードを取得
			$api_Key = $this->Apikey->getApiKey($shop_id);

			$socket = new HttpSocket();
            $i=0;
			
			foreach($articleidlist as $fvalue){ 
				
        		//もしもAPIで検索
				$param = '&article_id='. $fvalue;
				$url = 'http://api.moshimo.com/article/search.xml?authorization_code='. $api_Key. $param;
	
				$xmlarticle = $socket->get($url);
		        $article = simplexml_load_string($xmlarticle); 
		        $Totalcount=$article->Found;
		        
				if(!($Totalcount == 0)){
					foreach($article->Articles->Article as $data){
		        
						$item[$i]['article']['ArticleId']=trim($data->ArticleId);
	   			   	  	$item[$i]['article']['Name']=trim($data->Name);
	           	   	  	$item[$i]['article']['Description']=trim($data->Description);
	           	   	  	$item[$i]['article']['Description_b']=mb_substr(strip_tags($data->Description),0,120,'UTF-8').'...';
	           	   	  	$item[$i]['article']['SpecialDescription']=trim($data->SpecialDescription);
	           	   	  	$item[$i]['article']['Spec']=trim($data->Spec);
	           	   	  	$item[$i]['article']['CatchCopy']=trim($data->CatchCopy);
	           	   	  	$item[$i]['article']['ShopPrice']=trim($data->ShopPrice);
	           	   	  	$item[$i]['article']['ImageCode']=trim($data->ImageCode);
	           	   	  	$item[$i]['article']['ImageCount']=trim($data->ImageCount);
					}
					$item[$i]['article']['ShopId']=trim($article->Shop->ShopId);
					$i = $i + 1;
				}
			}
		}
		
		$this->set('htmlarticlelist',$item);
        $this->render('getHtmlAticleList');
	}
	
}
?>